<template>
  <el-dialog
    :model-value="visible"
    title="编辑客户信息"
    width="500px"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="客户ID" prop="id">
        <el-input v-model="form.id" disabled />
      </el-form-item>
      
      <el-form-item label="客户姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入客户姓名" />
      </el-form-item>
      
      <el-form-item label="客户类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择客户类型" style="width: 100%">
          <el-option label="企业客户" value="企业" />
          <el-option label="个人客户" value="个人" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="所属社区" prop="community">
        <el-input v-model="form.community" placeholder="请输入所属社区" />
      </el-form-item>
      
      <el-form-item label="经理" prop="manager">
        <el-input v-model="form.manager" placeholder="请输入经理姓名" />
      </el-form-item>
      
      <el-form-item label="专员" prop="specialist">
        <el-input v-model="form.specialist" placeholder="请输入专员姓名" />
      </el-form-item>
      
      <el-form-item label="社区经理" prop="communityManager">
        <el-input v-model="form.communityManager" placeholder="请输入社区经理姓名" />
      </el-form-item>
      
      <el-form-item label="行业ID" prop="industryId">
        <el-input v-model="form.industryId" placeholder="请输入行业ID" />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitLoading">
          确认修改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, reactive, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import { updateCustomerInfo } from '@/apis/customer'

export default {
  name: 'EditCustomer',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:visible', 'success'],
  setup(props, { emit }) {
    const formRef = ref()
    const submitLoading = ref(false)
    
    const form = reactive({
      id: '',
      name: '',
      type: '',
      community: '',
      manager: '',
      specialist: '',
      communityManager: '',
      industryId: ''
    })
    
    const rules = {
      name: [
        { required: true, message: '请输入客户姓名', trigger: 'blur' }
      ],
      type: [
        { required: true, message: '请选择客户类型', trigger: 'change' }
      ]
    }
    
    // 设置表单数据
    const setFormData = (data) => {
      // 检查传入的数据是否包含ID
      console.log('编辑客户数据:', data);
      Object.keys(form).forEach(key => {
        form[key] = data[key] || data.customerId || data.id || '';
      });
    }
    
    // 关闭弹窗
    const handleClose = () => {
      emit('update:visible', false)
    }
    
    // 提交表单
    const handleSubmit = async () => {
      try {
        await formRef.value.validate()
        submitLoading.value = true
        
        // 确保使用正确的字段名
        const requestData = {
          id: form.id,
          name: form.name,
          type: form.type,
          community: form.community,
          manager: form.manager,
          specialist: form.specialist,
          communityManager: form.communityManager,
          industryId: form.industryId
        }
        
        const res = await updateCustomerInfo(requestData)
        if (res.code === 0) {
          ElMessage.success('客户信息修改成功')
          emit('success')
          handleClose()
        } else {
          ElMessage.error(res.msg || '修改失败')
        }
      } catch (error) {
        console.error('修改客户信息失败:', error)
        ElMessage.error('修改客户信息失败')
      } finally {
        submitLoading.value = false
      }
    }
    
    defineExpose({
      setFormData
    })
    
    return {
      formRef,
      form,
      rules,
      submitLoading,
      setFormData,
      handleClose,
      handleSubmit
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>